package com.example.musicdemo.adapter


import android.content.Context
import android.database.Cursor
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.SectionIndexer
import androidx.cursoradapter.widget.CursorAdapter
import com.example.musicdemo.model.Music
import com.example.musicdemo.widget.VBangItemView
import java.util.*

class VBangAdapter(val context: Context?,var mList: List<Music>) : BaseAdapter(),
    SectionIndexer {

    var position: Int? = null

/*
    */
/**
     * 新建一个条目item
     *//*

    override fun newView(context: Context?, cursor: Cursor?, parent: ViewGroup?): View {
        return VBangItemView(context)
    }

    override fun bindView(view: View?, context: Context?, cursor: Cursor?) {
        val itemView = view as VBangItemView
        var itemBean :Music? =null
        if(mList.isNotEmpty()) {
             itemBean = position?.let { mList.get(it) }
        }
        val section = position?.let { getSectionForPosition(it) }

        if (position == section?.let { getPositionForSection(it) }) {
            itemView.tvLetter!!.visibility = View.VISIBLE
            if (itemBean != null) {
                itemView.tvLetter!!.setText(itemBean.sortLetters)
            }
        } else itemView.tvLetter!!.visibility = View.GONE

        if (itemBean != null) {
            itemView.setData(itemBean)
        }
    }
*/

    override fun getCount(): Int {
        return mList.size
    }

    override fun getItem(position: Int): Any {

        return mList?.get(position)

    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        this.position = position
        val view: View?
        var vBangView :VBangItemView? = null
        var music : Music? =null
        if (position<mList.size) {
            music = mList?.get(position)
            println("我想看看music name ${music.name}   还有@${music.sortLetters}")
        }
        if (convertView == null) {
            vBangView = VBangItemView(context)
            view = vBangView

        } else {
            view = convertView
        }
        val section = getSectionForPosition(position)

        if (position == getPositionForSection(section)) {
            (view as VBangItemView).tvLetter!!.visibility = View.VISIBLE
            view.tvLetter!!.text = music?.sortLetters
        } else {
            (view as VBangItemView).tvLetter!!.visibility = View.GONE
        }
        if (music != null) {
            (view as VBangItemView).setData(music)
        }
        return view


    }

    override fun getSections(): Array<Any>? {
        return null
    }

    override fun getPositionForSection(sectionIndex: Int): Int {
       if (mList.isEmpty()) return 0
        for (i in 0 until count) {
            val sortStr = mList[i].sortLetters
            if (sortStr?.isEmpty() == true) return 0
            val firstChar: Char? = sortStr?.uppercase(Locale.getDefault())?.get(0)

            if (firstChar?.code?.toByte()?.toInt() == sectionIndex) {
                return i
            }
        }
        return -1
    }

    override fun getSectionForPosition(position: Int): Int {
        if (mList.isEmpty()) return 0
        if (mList[position].sortLetters?.isEmpty() == true) return 0
        return mList[position].sortLetters?.get(0)?.code?.toByte()?.toInt()!!

    }


}